package com.wx.spring.mvc;

import com.wx.spring.annotations.MyRequstParam;
import com.wx.spring.exception.SpringMvcException;
import com.wx.spring.utils.AnnotationUtils;
import com.wx.spring.utils.ConvertUtis;
import com.wx.spring.utils.MyStringUtils;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Parameter;

/**
 * @ClassName BindingByMyRequstParam
 * @Author wx
 * @Description 获取注解MyRequstParam绑定的参数值
 * @Date 2019-01-30-18:23
 */
public class BindingByMyRequstParam implements  BindingParamter{

    /**
     * @methodName: bindingParamter
     * @author: wx
     * @description: 获取绑定参数对应的值
     * @param parameter
     * @param request
     * @date: 2019/1/30
     * @return: java.lang.Object
     */
    @Override
    public Object getBindingParamter(Parameter parameter, HttpServletRequest request) throws IllegalAccessException, InstantiationException, NoSuchMethodException {
        MyRequstParam myRequstParam = parameter.getAnnotation(MyRequstParam.class);
        if (AnnotationUtils.isEmpty(myRequstParam)){
            return null;
        }
        String myRequstParamValue = myRequstParam.value();
        String parameter1 = request.getParameter(myRequstParamValue);
        if (MyStringUtils.isEmpty(parameter1)) {
            throw new SpringMvcException("绑定参数异常");
        }
       return ConvertUtis.convert(parameter.getType().getSimpleName(),parameter1);
    }
}
